<?
class DB
{
    private $db_user;
    private $db_host;
    private $db_pass;
    private $db_name;
    private $result;
    private $logger;

    public $query;
	public $pstmt;
	
    public static $link;
    
    function __construct() {
        $this->db_host = Props::get('db', 'db_host');
        $this->db_user = Props::get('db', 'db_user');
        $this->db_pass = Props::get('db', 'db_pass');
        $this->db_name = Props::get('db', 'db_name');
        $this->logger = Factory::manufacture('Logger');
        $this->connect();
    }

    function connect() {
        if (self::$link) {
            $message = "Using existing db connection: ";
        } else {
            $message = "Creating new db connection to $this->db_name on $this->db_host: ";
            self::$link = mysqli_connect($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
        }

        if (mysqli_connect_errno()) {
            $message = "Connect failed: ". mysqli_connect_error();
            $this->logger->err($message);
            exit();
        } else {
            $this->logger->log($message ."#". mysqli_thread_id(self::$link),1 );
        }
    }

    function setQuery($query) {
        $this->query = $query;
        $this->logger->log("Query set to: ". $this->query ."\n");
        $this->result = self::$link->query($this->query);

        if (mysqli_errno(self::$link)) {
            $message = "Query failed: ". mysqli_error(self::$link);
            $this->logger->err($message);
        } elseif (isset($this->result->num_rows)) {
            $this->logger->log("Query returned ". $this->result->num_rows ." rows\n"); 
        }

        return $this->result;
    }

    function getError() {
        return mysqli_error(self::$link);
    }

    function getInsertId() {
        return mysqli_insert_id(self::$link);
    }

    function getNumRows() {
        return mysqli_num_rows($this->result);
    }
    
    function prepareStatement($sql, $param_types, &$params, &$results, $results_count) {

    	if ($this->pstmt) { 
    		closePreparedStatement();
    	}
    	
    	if (!($this->pstmt = self::$link->prepare($sql))) {
    		return false;
    	}
    	
    	if ($param_types) {
    		
    		$bindargs = array();
    		$bindargs[0] = $param_types;
    		for ($i = 0; $i < count($params); ++$i) {
    			$bindargs[$i+1] = &$params[$i];
    		}
    		 
    		call_user_func_array(array($this->pstmt, "bind_param"), $bindargs);
    		if (mysqli_errno(self::$link)) {
    			return false;
    		}
    	}

    	if ($results_count) {
    		$results_args = array();
    		 
    		for ($i = 0; $i < $results_count; ++$i) {
    			$results_args[$i] = &$results[$i];
    		}
    		
    		call_user_func_array(array($this->pstmt, "bind_result"), $results_args);
    		if (mysqli_errno(self::$link)) {
    			return false;
    		}
    	}
    	
     	return $this->pstmt;
    }
    
    function closePreparedStatement() { 
    	if ($this->pstmt) { 
    		$this->pstmt->close();
    		$this->pstmt = null;
    	}
    }
}
?>
